package com.dongdongshop.service.impl;

import com.dk.util.PageResult;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbgoodService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//增加商品信息
@Service
@DubboService
public class TbgoodServiceimpl implements TbgoodService {

    @Autowired
    TbGoodsMapper tgm;//商品信息表1

    @Autowired
    TbGoodsDescMapper tdm;//商品信息表2,大数据

    @Autowired
    TbItemMapper tim;//商品总表

    @Override
    public int insertgood(TbGoods tg, TbGoodsDesc tgd, List<TbItem> tbitem) {
        tgm.insertSelective(tg);//增加商品表1
        Long id = tg.getId();//返回的主键
        tgd.setGoodsId(id);
        tdm.insertSelective(tgd);////商品信息表2,大数据


        TbGoods tbGoods = tgm.selectByPrimaryKey(id);//增加的这条数据,商品表1
        TbGoodsDesc tgdes = tdm.selectByPrimaryKey(id);//增加的这条数据,商品表


        for(int i=0;i<tbitem.size();i++){
            tbitem.get(i).setTitle(tbGoods.getCaption());//标题
            tbitem.get(i).setSellPoint(tgdes.getIntroduction());//卖点
            tbitem.get(i).setCategoryid(tbGoods.getCategory3Id());//所属类目
            tbitem.get(i).setMarketPrice(tbGoods.getPrice());//市场价
            tbitem.get(i).setGoodsId(tbGoods.getId());//spu的id
            tbitem.get(i).setSpec(tgdes.getCustomAttributeItems());
            tim.insertSelective(tbitem.get(i)); //商品总表
        }
        return 0;
    }


    //查询全部
    @Override
    public PageResult getall(Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        List<TbGoods> ls = tgm.selectByExample(null);
        PageInfo info=new PageInfo(ls);
        PageResult p=new PageResult(pagenum,pagesize,info.getList(),info.getTotal());
        return p;



    }

    //3个重写方法，通过主键id查询
    @Override
    public TbGoods bygoods(Long id) {
        return tgm.selectByPrimaryKey(id);
    }
    @Override
    public TbGoodsDesc bygoodsdescid(Long id) {
        return tdm.selectByPrimaryKey(id);
    }
    //商品总表
    @Override
    public List<TbItem> byitemgoodid(Long id) {
        TbItemExample te=new TbItemExample();
        TbItemExample.Criteria criteria = te.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        return tim.selectByExample(te);
    }

    //审核通过
    @Override
    public int tongguo(List<TbGoods> tg) {

        for(int i=0;i<tg.size();i++){
            tgm.updateByPrimaryKeySelective(tg.get(i));
        }

        return 1;
    }


    //通过主键查询数据
    @Override
    public TbItem selectByid(Long skuId) {
        return tim.selectByPrimaryKey(skuId);
    }
}
